/* 
 * Copyright (C) 2012 UTSChedule
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package utsc.apps.utschedule.ui.activities.adapters;

import java.util.ArrayList;
import java.util.List;

import utsc.apps.utschedule.classes.Assignment;
import utsc.apps.utschedule.classes.Exam;
import utsc.apps.utschedule.classes.GenericTask;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

/**
 * This is used for the {@link GenericTask task} list in the course grade forecast feature.
 *
 */
public class TaskAdapter extends ArrayAdapter<GenericTask> {

	private int _textViewResourceId;
	private ArrayList<GenericTask> _taskList;
	private static final String REMAINING_TEXT="Remaining %s";
	
	public TaskAdapter(Context context, int textViewResourceId,
			List<GenericTask> objects) {
		super(context, textViewResourceId, objects);
		_taskList=new ArrayList<GenericTask>(objects);
		_textViewResourceId=textViewResourceId;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ViewHolder holder;
		if (convertView==null) {
			holder=new ViewHolder();
			convertView=LayoutInflater.from(getContext()).inflate(_textViewResourceId, null);
			holder._txtTask=(TextView) convertView.findViewById(android.R.id.text1);
			convertView.setTag(holder);
		}else {
			holder=(ViewHolder) convertView.getTag();
		}
		final GenericTask task=getItem(position);
		if (task instanceof Assignment || task instanceof Exam) {
			holder._txtTask.setText(task.toString()+" "+task.get_weight()+"%");
		}
		else {
			holder._txtTask.setText(String.format(REMAINING_TEXT, task.get_weight())+"%");
		}
		
		return convertView;
	}
	
	@Override
	public GenericTask getItem(int position) {
		if (_taskList!=null) {
			return _taskList.get(position);
		}
		return null;
	}
	
	private static class ViewHolder{
		TextView _txtTask;
	}
	
	public void set_courseList(ArrayList<GenericTask> _taskList) {
		this._taskList = _taskList;
		
	}
	

}
